<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>菜单管理</title>
<!-- 导入jquery核心类库 -->
<%@include file="../include/jqueryeasyui.jsp"%>
<!-- 导入ztree类库 -->
<link rel="stylesheet"
	href="${pageContext.request.contextPath }/js/ztree/zTreeStyle.css"
	type="text/css" />
<script
	src="${pageContext.request.contextPath }/js/ztree/jquery.ztree.all-3.5.js"
	type="text/javascript"></script>

<script type="text/javascript">

/* function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
} */


var sessionStaff=<%=session.getAttribute("staff")%>;  //sLoginName、menuIds
 

// 初始化ztree菜单
$(function() {
	/* var sid = getQueryString("sid"); */
	var sid = $('#input_sid').val();
	var menuIds = $('#input_menuids').val();
	var userType = $('#userType').val();
	
	
	
	
	 $('#save').click(function(){
		
		 //组装参数
		 var ids = new String();

		 var treeObj = $.fn.zTree.getZTreeObj("treeMenu"); 
		 var selectedNode = treeObj.getCheckedNodes();
		 
		 for(var i=0;i<selectedNode.length;i++){
		 	if(i==0){
		 		ids+=selectedNode[i].mId;
		 	}else{
		 		ids+=','+selectedNode[i].mId;
		 	}
		 }
		 
		 if(ids.length==0){
			 $.messager.confirm('提示', '确认要取消该用户所有权限？',  function(data) {
				if (data==false){
					return;
				}else if(data==true){
					changeQuanXian();
				}
				
			});
		 }else{
			 changeQuanXian();
		 }
		 
	
		 function changeQuanXian() {
			 //发送网络请求
				$.ajax({						
						url : '${pageContext.request.contextPath}/page.staffAuthorization.ashx',
						type : 'POST',
						dataType : 'text',
						data : {
							sid:sid,
							ids:ids,
							oldids:$("#input_oldMenuids").val()
						},
						success : function(data) {
							if(data==1){
								if(userType=='3'){
									location.href="${pageContext.request.contextPath}/page.role.ashx"
									
								}else{
									location.href="${pageContext.request.contextPath}/page.kuguan.ashx"
								}
 							}else{
								$.messager.alert("提示", "赋权失败，请重试", "info");
							}
						},
						error : function(msg) {
							alert('赋权失败!');
						}
					}); 
		}
		 
	 });
	 
	
	
	$('#return').click(function(){
 		
		if(userType=='3'){
			location.href="${pageContext.request.contextPath}/page.role.ashx"
			
		}else{
			location.href="${pageContext.request.contextPath}/page.kuguan.ashx"
		}
		 
		

 });
	var setting = {
			 check: {  
	                enable: true,  
	            },
		data : {
			simpleData : { // 简单数据 
				enable : true,
				idKey:"mCode",
				pIdKey:"mPId",
				
			}, 
			key: {
				name:"mName",
			}
		},
		callback : {
			//onClick : onClick
			//onCheck:onCheck
		}
	};
 	
	var url;
	if(sessionStaff.sLoginName=="admin"){
		url = '${pageContext.request.contextPath}/page.menusList.ashx';
	}else {
		//url = '${pageContext.request.contextPath}/page.staffMenusList.ashx';
		url = '${pageContext.request.contextPath}/page.sessionStaffAndChoosenStaffMenusList.ashx';
		
	}
		

		// 基本功能菜单加载
		$.ajax({
			url : url,
			data:{'menuIds':menuIds},
			type : 'POST',
			dataType : 'text',
			success : function(data) {
  				var zNodes = eval("(" + data + ")");
				
				$.fn.zTree.init($("#treeMenu"), setting, zNodes);
				var treeObj = $.fn.zTree.getZTreeObj("treeMenu"); 
				treeObj.expandAll(true);
				var menuidsArr = menuIds.split(','); 
				for(var i = 0;i<menuidsArr.length;i++){  
				    //treeObj.checkNode(treeObj.getNodesByParam("mId", menuidsArr[i],null)[0],true, true);  
				    //treeObj.getNodesByParam("mId", menuidsArr[i],null)[0].checked=true; 
			    	if(treeObj.getNodesByParam("mId", menuidsArr[i],null)[0]){
			    		treeObj.checkNode(treeObj.getNodesByParam("mId", menuidsArr[i],null)[0]); 
			    	}
				} 
			},
			error : function(msg) {
				alert('菜单加载异常!');
			}
		});
	
	 
});



	
</script>	
</head>
<body class="easyui-layout" >
<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
		<div class="datagrid-toolbar">
			<a id="return" icon="icon-undo" href="#" class="easyui-linkbutton" plain="true" >返回</a>
			<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存并返回</a>
			
		</div>
	</div>
	<div region="center">
		<ul id="treeMenu" class="ztree"></ul>
		<input type="hidden" id="input_oldMenuids" value="${staff.oldMemuIds}"/>
	</div>
	<input type="hidden" id="input_sid" value="${staff.sId}"/>
	<input type="hidden" id="input_menuids" value="${staff.menuIds}"/>
	<input type="hidden" id="input_oldMenuids" name = 'input_oldMenuids' value="${staff.oldMemuIds}"/>
	<input type="hidden" id="userType" value="${staff.userType}"/>
</body>
</html>